//
//  ExaminationMainController.m
//  Arts1
//
//  Created by apple on 15/9/15.
//  Copyright (c) 2015年 Arts1. All rights reserved.
//

#import "ExaminationMainController.h"
#import "NSDate+FSExtension.h"
#import "ExaminationTitleDate.h"
#import "ExaminationModel.h"
#import "SSLunarDate.h"
#import "AllHeader.h"

#define kPink [UIColor colorWithRed:198/255.0 green:51/255.0 blue:42/255.0 alpha:1.0]
#define kBlue [UIColor colorWithRed:31/255.0 green:119/255.0 blue:219/255.0 alpha:1.0]
#define kBlueText [UIColor colorWithRed:14/255.0 green:69/255.0 blue:221/255.0 alpha:1.0]

//#define kURL @"http://arts1.dlab.com.cn/app/myexam"
#define kURL @"http://arts1.cn/app/myexam"

@interface ExaminationMainController ()<UIScrollViewDelegate, FSCalendarDataSource, FSCalendarDelegate>

@property (strong, nonatomic) NSCalendar *currentCalendar;
@property (strong, nonatomic) SSLunarDate *lunarDate;
@property (nonatomic ,strong) NSMutableArray *dataArray;

@end

@implementation ExaminationMainController

- (NSMutableArray *)dataArray
{
    if (_dataArray == nil) {
        self.dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kBackgroundColor;
    
    
//    _currentCalendar = [NSCalendar currentCalendar];
    
    _calendar = [[FSCalendar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 240 * HEIGHT)];
    _calendar.backgroundColor = kBackgroundColor;
    self.tableView.tableHeaderView = _calendar;
    _calendar.delegate = self;
    _calendar.userInteractionEnabled = YES;
//    _calendar.dataSource = self;
    _calendar.allowsMultipleSelection = YES;
    
    [self requestData];
}

/**
 *  请求数据
 */
- (void)requestData
{
    AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
    manage.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    manage.responseSerializer = [AFHTTPResponseSerializer serializer];
    //获取uid和key
    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"dataForKeyAndUid"];
    
    NSString *uid = dic[@"data"][@"uid"];
    NSString *key = dic[@"data"][@"key"];
    NSDictionary *pardic = [NSDictionary dictionary];
    
    
    if (uid && key)
    {
        pardic = @{@"uid":uid,
                   @"key":key};
    }
    
    [manage POST:kURL parameters:pardic
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
             int status = [dic[@"status"] intValue];
             if (status == 1) {
                 NSArray *list = dic[@"list"];
                 for (NSDictionary *dict in list) {
                     ExaminationModel *model = [[ExaminationModel alloc] init];
                     [model setValuesForKeysWithDictionary:dict];
                     [self.dataArray addObject:model];
                     dispatch_async(dispatch_get_main_queue(), ^{
                         NSDate *date = [NSDate dateWithTimeIntervalSince1970:[model.examTime intValue] - 86400];
                         [_calendar selectDate:date];
                     });
                     
                 }
                 [self.tableView reloadData];
             }
             // 请求成功的时候调用这个block
             NSLog(@"数据请求--%@", dic);
         } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             // 请求失败的时候调用调用这个block
             NSLog(@"请求失败");
         }];
}
- (void)calendar:(FSCalendar *)calendar didSelectDate:(NSDate *)date
{
    //格式化时间
    NSDateFormatter *formater = [[NSDateFormatter alloc] init];
    //设置格式
    [formater setDateFormat:@"MM月dd日"];
    //获取系统的时间区域
    formater.timeZone = [NSTimeZone systemTimeZone];
    //设定本地化，时间会按照本地的习惯和文字显示日期
    formater.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en-us"];
    NSString *string = [formater stringFromDate:date];
    NSLog(@"%@",string);
}

- (BOOL)calendar:(FSCalendar *)calendar shouldDeselectDate:(NSDate *)date
{
    return YES;
}

- (BOOL)calendar:(FSCalendar *)calendar shouldSelectDate:(NSDate *)date
{
    return YES;
}

- (void)setTheme:(NSInteger)theme
{
    if (_theme != theme) {
        _theme = theme;
        //        _calendar.appearance.weekdayTextColor = [UIColor redColor];
        //        _calendar.appearance.headerTitleColor = [UIColor redColor];
        //        _calendar.appearance.eventColor = [UIColor greenColor];
        //        _calendar.appearance.selectionColor = [UIColor blueColor];
        //        _calendar.appearance.headerDateFormat = @"yyyy/MM";
        _calendar.appearance.todayColor = [UIColor orangeColor];
        _calendar.appearance.cellStyle = FSCalendarCellStyleRectangle;
        //        _calendar.appearance.headerMinimumDissolvedAlpha = 1.0;
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setLunar:(BOOL)lunar
{
    if (_lunar != lunar)
    {
        _lunar = lunar;
        [_calendar reloadData];
    }
}

#pragma mark - Table view data source

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"考试";
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ExaminationTitleDate *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[ExaminationTitleDate alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    ExaminationModel *model = _dataArray[indexPath.row];
    cell.model = model;
    
    // Configure the cell...
    
    return cell;
}

@end
